import tkinter as tk
import pywinauto
import tkinter.messagebox as messagebox
from time import sleep, time
from threading import Timer
import random, json, pyperclip, os, pyautogui, win32gui, win32con


class BakuPetController:
  def __init__(self, tk_root) -> None:
    self.tk_root = tk_root
    self.build_ui()
    self.timer_macro = None
    self.timer_auto_pet = None
    self.timer_focus_wow = None
    self.timer_move_mouse = None
    self.timer_press_esc = None

  def build_ui(self):
    tk.Button(self.tk_root, text="启动巴库1", command=self.run_task).place(x=10, y=90, width=80, height=30)
    tk.Button(self.tk_root, text="停止巴库1", command=self.stop_task).place(x=100, y=90, width=80, height=30)
    tk.Button(self.tk_root, text="Debug", command=self.debug).place(x=190, y=90, width=80, height=30)

  def debug(self):
    wow_handle = win32gui.FindWindow(None, '魔兽世界')
    win32gui.SetForegroundWindow(wow_handle)
    sleep(1)
    pyautogui.press("ESC")
    print("22222")
    return
    left, top, right, bottom = win32gui.GetWindowRect(wow_handle)
    pyautogui.moveTo(left + random.randint(0, right - left), top + random.randint(0, bottom - top), duration=0.25)

  def run_task(self):
    self.run_focus_wow()
    self.run_macro()
    self.run_auto_pet()
    self.start_press_esc_timer()
    print("开始执行任务")

  def start_press_esc_timer(self):
    self.stop_press_esc_timer()
    pyautogui.press("ESC")
    print("press esc")
    self.timer_press_esc = Timer(500, self.start_press_esc_timer)
    self.timer_press_esc.start()

  def stop_press_esc_timer(self):
    if self.timer_press_esc:
      self.timer_press_esc.cancel()
      self.timer_press_esc = None


  def run_move_mouse(self):
    wow_handle = win32gui.FindWindow(None, '魔兽世界')
    left, top, right, bottom = win32gui.GetWindowRect(wow_handle)
    
  def stop_move_mouse(self):
    pass

  def run_focus_wow(self):
    self.stop_focus_wow()
    wow_handle = win32gui.FindWindow(None, '魔兽世界')
    if not wow_handle:
      wow_handle = win32gui.FindWindow(None, '魔獸世界')
    win32gui.SetForegroundWindow(wow_handle)
    self.timer_focus_wow = Timer(random.uniform(5, 10), self.run_focus_wow)
    self.timer_focus_wow.start()

  def stop_focus_wow(self):
    if self.timer_focus_wow:
      self.timer_focus_wow.cancel()
      self.timer_focus_wow = None
    
  def run_macro(self):
    self.stop_macro()
    pyautogui.press("8")
    self.timer_macro = Timer(random.uniform(1.1, 2.4), self.run_macro)
    self.timer_macro.start()

  def stop_macro(self):
    if self.timer_macro:
      self.timer_macro.cancel()
      self.timer_macro = None

  def run_auto_pet(self):
    self.stop_auto_pet()
    pyautogui.press("9")
    self.timer_auto_pet = Timer(random.uniform(0.5, 1.5), self.run_auto_pet)
    self.timer_auto_pet.start()
  
  def stop_auto_pet(self):
    if self.timer_auto_pet:
      self.timer_auto_pet.cancel()
      self.timer_auto_pet = None

  def stop_task(self):
    self.stop_macro()
    self.stop_auto_pet()
    self.stop_focus_wow()
    self.stop_press_esc_timer()
    print("停止执行任务")
    